<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;

/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        //查询今日的数据
        $info = Db::name('contents')
            ->alias('c')
            ->join('fa_bingzhong b', 'c.bingzhong_id = b.id')
            ->where('c.status', 'normal')
            ->where('c.state', '1')
            ->where('c.deletetime', null)
            ->where('b.status', 'normal')
            ->where('b.state', '1')
            ->where('b.deletetime', null)
            ->where('c.uploaddate', date('Y-m-d', time()))
            ->order('b.weigh desc')
            ->select();
        //最下方加上汇总数据
        $info = $this->arr_huizhong($info);
        $this->assign('info', $info);

        //查询本周数据
        $weekinfo = Db::name('contents')
            ->alias('c')
            ->join('fa_bingzhong b', 'c.bingzhong_id = b.id')
            ->where('c.status', 'normal')
            ->where('c.state', '1')
            ->where('c.deletetime', null)
            ->where('b.status', 'normal')
            ->where('b.state', '1')
            ->where('b.deletetime', null)
            ->whereTime('c.uploaddate', 'w')
            ->order('b.weigh desc')
            ->select();

        //处理数据
        $weekinfo = $this->arr_juhe($weekinfo);
        
        //最下方加上汇总数据
        $weekinfo = $this->arr_huizhong($weekinfo);

        $this->assign('weekinfo', $weekinfo);

        return $this->view->fetch();
    }

    /**
     * 汇总数据
     *
     */
    protected function arr_huizhong($info)
    {
        //汇总操作
        //消费
        $xiaofei = 0;
        //点击
        $dianji = 0;
        //展现
        $zhanxian = 0;
        //对话
        $duihua = 0;
        //有效
        $youxiao = 0;
        //预约
        $yuyue = 0;
        //到诊
        $daozheng = 0;

        foreach ($info as $k => $v) {
            $xiaofei += $v['xiaofei'];
            $dianji += $v['dianji'];
            $zhanxian += $v['zhanxian'];
            $duihua += $v['duihua'];
            $youxiao += $v['youxiao'];
            $yuyue += $v['yuyue'];
            $daozheng += $v['daozheng'];
        }

        if ($xiaofei) {
            array_push($info, array(
                'name' => '汇总',
                'xiaofei' => $xiaofei,
                'junjia' => round($xiaofei / $dianji, 2),
                'dianji' => $dianji,
                'zhanxian' => $zhanxian,
                'dianjilv' => round($dianji / $zhanxian, 2),
                'duihua' => $duihua,
                'youxiao' => $youxiao,
                'yuyue' => $yuyue,
                'daozheng' => $daozheng,
                'dhcb' => round($xiaofei / $youxiao, 2),
                'yycb' => round($xiaofei / $yuyue, 2),
                'dzcb' => round($xiaofei / $daozheng, 2),
            ));
        }

        return $info;
    }

    /**
     * 处理局和数据
     *
     */
    protected function arr_juhe($weekinfo)
    {
        $temparr = array();
        foreach ($weekinfo as $v) {
            //查询 bingzhong_id,有的话,就添加,没有就加入数组中
            if (!array_key_exists($v['bingzhong_id'], $temparr)) {
                $temparr[$v['bingzhong_id']] = $v;
            } else {
                $temparr[$v['bingzhong_id']]['xiaofei'] += $v['xiaofei'];
                $temparr[$v['bingzhong_id']]['dianji'] += $v['dianji'];
                $temparr[$v['bingzhong_id']]['zhanxian'] += $v['zhanxian'];
                $temparr[$v['bingzhong_id']]['duihua'] += $v['duihua'];
                $temparr[$v['bingzhong_id']]['youxiao'] += $v['youxiao'];
                $temparr[$v['bingzhong_id']]['yuyue'] += $v['yuyue'];
                $temparr[$v['bingzhong_id']]['daozheng'] += $v['daozheng'];
            }

            $tempnum = count($temparr);
            //均价
            $temparr[$v['bingzhong_id']]['junjia'] = round($temparr[$v['bingzhong_id']]['xiaofei'] / $temparr[$v['bingzhong_id']]['dianji'], 2);
            //点击率
            $temparr[$v['bingzhong_id']]['dianjilv'] = round($temparr[$v['bingzhong_id']]['dianji'] / $temparr[$v['bingzhong_id']]['zhanxian'], 2);
            //对话成本
            $temparr[$v['bingzhong_id']]['dhcb'] = round($temparr[$v['bingzhong_id']]['xiaofei'] / $temparr[$v['bingzhong_id']]['youxiao'], 2);
            //预约成本
            $temparr[$v['bingzhong_id']]['yycb'] = round($temparr[$v['bingzhong_id']]['xiaofei'] / $temparr[$v['bingzhong_id']]['yuyue'], 2);
            //到诊成本
            $temparr[$v['bingzhong_id']]['dzcb'] = round($temparr[$v['bingzhong_id']]['xiaofei'] / $temparr[$v['bingzhong_id']]['daozheng'], 2);

        }
        return $temparr;
    }

}
